package top.saplf.sileny.ext

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import top.saplf.sileny.SilenyApplication

/**
 * @author Saplf on 17/4/12
 */
val cacheSharedPref: SharedPreferences by lazy { SilenyApplication.app.getSharedPreferences("cache", Context.MODE_PRIVATE) }

@SuppressLint("CommitPrefEdits")
inline fun SharedPreferences.write(action: SharedPreferences.Editor.() -> Unit) = with(edit()) {
    action()
    apply()
}

@SuppressLint("CommitPrefEdits")
inline fun <T> SharedPreferences.writeSingle(action: SharedPreferences.Editor.() -> Pair<String, T>)
        = with(edit()) {
    val pair = action()
    when(pair.second) {
        is Boolean -> putBoolean(pair.first, pair.second as Boolean)
        is String -> putString(pair.first, pair.second as String)
        else -> {}
    }
    apply()
}
